// 导入api接口
import { delImgStaff, deptEmpInfoAdd, deptEmpInfoDelete, deptEmpInfoImgList, deptEmpInfoList, deptEmpInfoUpdate, listDept, listTeam } from '@/api/api'
import { dateRevert, imgFilter } from '@/utils/businessTools'
import configJs from '@/utils/configJs'
import { mapGetters } from 'vuex'
import defaultData from './data'
var data = JSON.parse(JSON.stringify(defaultData))
var adapter = {
	data,
	methods: {
		// ...mapActions(['logOut']),
		/**
		 * 初始化
		 */
		initPage(){
			//还原页面对象
			configJs.revert(data, defaultData);
		},
		
		/**
		 * 查询员工列表
		 */
		async search(val) {
			if (!this.$store.getters.pageAuth.authoritySearch) {
				return
			}
			console.log(val+"页数")
			this.revertActive() // 重置选择行
			let params = {...this.staffSearchObj[0]}
			// 处理时间
			params.sfzyxjzrq = dateRevert(params.sfzyxjzrq)
			params.rzrq = dateRevert(params.rzrq)
			params.cyzgzyxjzrq = dateRevert(params.cyzgzyxjzrq)
			params.jszcclzrq = dateRevert(params.jszcclzrq)
			params.jszyxjzrq = dateRevert(params.jszyxjzrq)
			// params.page = this.page.currPage
			typeof val != 'number' && (val = 1)
			params.page = val
			const res = await deptEmpInfoList(params)
			if (res.code === 0) {
			  this.page = res.data
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 刷新
		 */
		async refresh() {
			this.staffSearchObj = [{
				bm: '',
				xz: '',
				xm: '',
				nl: '',
				zwzz: '',
				sfzh: '',
				sfzyxjzrq: ['',''],
				zz: '',
				lxdh: '',
				rzrq: ['',''],
				cyzgzyxjzrq: ['',''],
				cyzgzbh: '',
				dabh: '',
		
				zjcx: '',
				jszcclzrq: '',
				jszyxq: '',
				sfba: '',
				bacl: '',
				bz: ''
			}]
			this.search()
		},

		/**
		 * 增加
		 */
		add() {
			this.resetAdd()
			this.addVisi = true
		},

		/**
		 * 重置增加弹窗
		 */
		resetAdd() {
			this.addStaff = {
				bm: '',
				xz: '',
				xm: '',
				nl: '',
				zwzz: '',
				sfzh: '',
				sfzyxjzrq: '',
				zz: '',
				lxdh: '',
				rzrq: '',
				cyzgzyxjzrq: '',
				cyzgzbh: '',
				dabh: '',
		
				zjcx: '',
				jszcclzrq: '',
				jszyxjzrq: '',
				sfba: '',
				bacl: '',
				bz: ''
			}
		},

		/**
		 * 增加点击
		 */
		async addClick() {
			const valid = await this.$refs['addStaff'].validate() // 校验
			if(valid) {
				const res = await deptEmpInfoAdd(this.addStaff)
				if (res.code === 0) {
					this.$message('添加成功')
					this.addVisi = false
					this.refresh()
				} else {
					this.$message(res.msg)
				}
			}
		},

		 /**
			* 修改
		  */
		edit(staff) {
			this.editStaff = staff
			this.editVisi = true
		},

		/**
		 * 日期转换
		 */
		dateFormat(date) {
			if ('' === date) {
				return date
			} else {
				if (typeof date == 'String') {
					let arr = date.split('_')
					return arr
				} else {
					if (Array.isArray(date) && date.length == 2) {
						return date[0] + '_' + date[1]
					} else {
						return date
					}
				}
			}
		},

		/**
		 * 修改点击
		 */
		async editClick() {
			const res = await deptEmpInfoUpdate(this.editStaff)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 删除
		 */
		async deleteClick(id) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await deptEmpInfoDelete(id)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 点击行
		 */
		rowClick(row, column, e) {
			this.activeStaff = row
			this.getImgList()
		},

		/**
		 * 重置选择行
		 */
		revertActive() {
			this.activeStaff = {
				id: '',
				bm: '',
				xz: '',
				xm: '',
				nl: '',
				zwzz: '',
				sfzh: '',
				sfzyxq: '',
				zz: '',
				lxdh: '',
				rzrq: '',
				cyzgzyxq: '',
				cyzgzbh: '',
				dabh: '',
		
				zjcx: '',
				jszcclzrq: '',
				jszyxq: '',
				sfba: '',
				bacl: '',
				bz: ''
			}
			this.activeImgList = []
		},

		/**
		 * 日期选择
		 */
		 datePick() {
			 console.error('datepick')
			 console.log(arguments)
			 return 1
		 },

		/**
		 * 部门列表
		 */
		async getlistDept() {
			const res = await listDept()
			if (res.code === 0) {
			  this.deptList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 小组列表
		 */
		async getlistTeam() {
			const res = await listTeam()
			if (res.code === 0) {
			  this.teamList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 获取图片列表
		 */
		async getImgList() {
			const res = await deptEmpInfoImgList(this.activeStaff.id)
			if (res.code === 0) {
				this.activeImgList = imgFilter(res.data.list)
			}
		},

		/**
		 * 删除图片
		 */
		async imgDel(fileName) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await delImgStaff(fileName)
			if (res.code === 0) {
				this.$message('删除成功')
				this.getImgList()
			} else {
				this.$message(res.msg)
			}
		}
	},
	computed: {
		...mapGetters(['pageAuth']),
	}
}
export default adapter
